package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.controller.demo.domain.CarProcess;
import com.ruoyi.web.controller.demo.domain.CarUseRecord;
import com.ruoyi.web.controller.demo.domain.SysWzxUser;
import com.ruoyi.web.controller.mapper.CarProcessMapper;
import com.ruoyi.web.controller.service.ICarUseRecordService;
import com.ruoyi.web.controller.service.ISysWzxUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.demo.domain.CarList;
import com.ruoyi.web.controller.service.ICarListService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆列Controller
 * 
 * @author ruoyi
 * @date 2022-12-25
 */
@Controller
@RequestMapping("/system/carList")
public class CarListController extends BaseController
{
    private String prefix = "system/carList";

    @Autowired
    private ICarListService carListService;
    @Autowired
    private ISysWzxUserService sysWzxUserService;
    @Autowired
    private ICarUseRecordService carUseRecordService;
    @Autowired
    private CarProcessMapper carProcessMapper;
    @RequiresPermissions("system:carList:view")
    @GetMapping()
    public String carList()
    {
        return prefix + "/carList";
    }

    /**
     * 查询车辆列列表
     */
    @RequiresPermissions("system:carList:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarList carList)
    {
        startPage();
        List<CarList> list = carListService.selectCarListList(carList);
        return getDataTable(list);
    }

    /**
     * 导出车辆列列表
     */
    @RequiresPermissions("system:carList:export")
    @Log(title = "车辆列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarList carList)
    {
        List<CarList> list = carListService.selectCarListList(carList);
        ExcelUtil<CarList> util = new ExcelUtil<CarList>(CarList.class);
        return util.exportExcel(list, "车辆列数据");
    }

    /**
     * 新增车辆列
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        //给前端输入收件人列表
        List<SysWzxUser> list = sysWzxUserService.selectSysWzxUserList(null);
        model.addAttribute("user", list);
        SysUser sysUser = getSysUser();
        model.addAttribute("sysUser", sysUser);
        return prefix + "/add";
    }

    /**
     * 新增保存车辆列
     */
    @RequiresPermissions("system:carList:add")
    @Log(title = "车辆列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarList carList, CarUseRecord carUseRecord, CarProcess carProcess)
    {
        carList.setCreatedTime(new Date());
        carListService.insertCarList(carList);
        if ("外借".equals(carList.getStatus())){
            carProcess.setUsedCar(carList.getCarId());
            carProcess.setCreater(getSysUser().getUserId());
            carProcess.setCreatedTime(new Date());
            carProcess.setStatus("未归还");
            carProcessMapper.insertCarProcess(carProcess);
        }
        carUseRecord.setRecordCar(carList.getCarId());
        carUseRecord.setOperationName("新增");
        carUseRecord.setCreater(getSysUser().getUserId());
        carUseRecord.setCreatedTime(new Date());
        return toAjax( carUseRecordService.insertCarUseRecord(carUseRecord));
    }

    /**
     * 修改车辆列
     */
    @RequiresPermissions("system:carList:edit")
    @GetMapping("/edit/{carId}")
    public String edit(@PathVariable("carId") Long carId, ModelMap mmap,Model model)
    {
        //给前端输入收件人列表
        List<SysWzxUser> list = sysWzxUserService.selectSysWzxUserList(null);
        model.addAttribute("user", list);
        SysUser sysUser = getSysUser();
        model.addAttribute("sysUser", sysUser);
        CarList carList = carListService.selectCarListByCarId(carId);
        mmap.put("carList", carList);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆列
     */
    @RequiresPermissions("system:carList:edit")
    @Log(title = "车辆列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarList carList,CarUseRecord carUseRecord)
    {
        carUseRecord.setRecordCar(carList.getCarId());
        carUseRecord.setOperationName("修改");
        carUseRecord.setCreater(getSysUser().getUserId());
        carUseRecord.setCreatedTime(new Date());
        carUseRecordService.insertCarUseRecord(carUseRecord);
        return toAjax(carListService.updateCarList(carList));
    }

    /**
     * 删除车辆列
     */
    @RequiresPermissions("system:carList:remove")
    @Log(title = "车辆列", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids,CarUseRecord carUseRecord)
    {
//        carUseRecord.setRecordCar((long) Integer.parseInt(ids));
      /*  CarList carList = carListService.selectCarListByCarId((long) Integer.parseInt(ids));
        carUseRecord.setName(carList.getName());
        carUseRecord.setColor(carList.getColor());
        carUseRecord.setCard(carList.getCard());
        carUseRecord.setOperationName("删除");
        carUseRecord.setCreater(getSysUser().getUserId());
        carUseRecord.setCreatedTime(new Date());
        carUseRecordService.insertCarUseRecord(carUseRecord);*/
        return toAjax(carListService.deleteCarListByCarIds(ids));
    }
}
